package io.xxx.omni.osc.sync

import com.alibaba.fastjson.JSONObject
import io.xxx.omni.osc.common.dateTimeFormatter
import io.xxx.omni.osc.common.toJSONObject
import io.xxx.omni.osc.common.toLocalDateTime
import io.xxx.omni.osc.domain.Document
import io.xxx.omni.osc.domain.DocumentType
import io.xxx.omni.osc.domain.PlatformIdentity
import io.xxx.omni.osc.domain.Store
import io.xxx.omni.osc.sdk.YjClient
import org.springframework.stereotype.Component
import java.time.Duration
import java.time.LocalDateTime

/**
 * 云集
 */
// POP订单同步: http://open.yunjiglobal.com/consoleweb/documentCenter/e459934c83414ea283d3de0e535f9d4d
// 签名: http://open.yunjiglobal.com/consoleweb/documentCenter/d641aff39bbb4a4f997edd97a21f00d5
abstract class YjPorter : GenericPageDocumentPorter(), PlatformIdentity.YunJi {

    protected abstract val method: String

    override fun getResponse(context: JobContext, pageNo: Int, hasTotal: Boolean): JSONObject {
        val (store, startTime, endTime, parameter) = context
        val request = buildRequest(startTime, endTime, parameter, pageNo)
        val client = applicationContext.getBean(YjClient::class.java)
        val response = client.execute(store, method, request)
        if (response.getString("flag") == "failure") {
            throwException(context, method, response)
        }
        return response.getJSONObject("data")
    }

    private fun buildRequest(
        startTime: LocalDateTime,
        endTime: LocalDateTime,
        parameter: Any?,
        pageNo: Int,
    ): Map<String, Any> {
        val request = mutableMapOf<String, Any>(
            "query_type" to 1,
            "start_modified" to startTime.format(dateTimeFormatter),
            "end_modified" to endTime.format(dateTimeFormatter),
            "page_no" to pageNo,
            "page_size" to pageSize,
        )
        if (parameter != null)
            request["status"] = parameter
        return request
    }
}

// 订单
// http://open.yunjiglobal.com/consoleweb/theApiCenter/10034
@Component
class YjTradePorter : YjPorter() {

    override val documentType = DocumentType.TRADE

    override val method = "pop.order.list.get"

    override fun getParameters(store: Store): Pair<List<Any>, Boolean> = listOf(40, 50, 60, 70) to false

    override fun getCount(context: JobContext, response: JSONObject): Int = response.getInteger("total")

    override fun buildDocuments(context: JobContext, response: JSONObject): List<Document> {
        return response
            .getJSONArray("lists")
            .map {
                val value = it.toJSONObject()
                val sn = value.getString("order_id")
                val status = value.getString("status")
                val modified = value.getLong("modify_time").toLocalDateTime()
                Document(sn, status, modified, value)
            }
    }
}

// 退单
// http://open.yunjiglobal.com/consoleweb/theApiCenter/10051
@Component
class YjRefundPorter : YjPorter() {

    override val documentType = DocumentType.REFUND

    override val method = "pop.refund.list.get"

    override val duration: Duration = Duration.ofHours(12)

    override fun getCount(context: JobContext, response: JSONObject): Int = response.getInteger("total_results")

    override fun buildDocuments(context: JobContext, response: JSONObject): List<Document> {
        return response
            .getJSONArray("refunds")
            .map {
                val value = it.toJSONObject()
                val sn = value.getString("refund_id")
                val rsn = value.getString("order_id")
                val status = value.getString("status")
                val modified = value.getLong("modify_time").toLocalDateTime()
                Document(sn, rsn, status, modified, value)
            }
    }
}